<?php

namespace frontend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;
use yii\web\IdentityInterface;
use common\models\member\Member;

use common\models\grade\Grade;
/**
 * This is the model class for table "articles".
 *
 * @property integer $id
 * @property string $title
 * @property string $subhead
 * @property string $intro
 * @property string $img
 * @property string $author
 * @property string $reason
 * @property string $link
 * @property string $content
 * @property string $seocontent
 * @property string $seokeyword
 * @property string $isrecom
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class ArtForm extends \yii\db\ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
	
	const DIS_DELETED = 20; //再审核
    const DIS_ACTIVE = 0; //待审核
    const DIS_SUCCESS =40; //已审核
    private $_statusLabel;
	private $_gradeLabel;
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'art';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','member_id'], 'required'],
            [['intro', 'content'], 'string'],
            [['status', 'display', 'created_at', 'updated_at'], 'integer'],
            [['title', 'subhead', 'img', 'author', 'reason', 'link', 'seocontent', 'seokeyword'], 'string', 'max' => 255],
            [['title'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'title' => Yii::t('app', '标题'),
            'subhead' => Yii::t('app', '副标题'),
            'intro' => Yii::t('app', '简介'),
            'img' => Yii::t('app', '图片'),
            'author' => Yii::t('app', '作者'),
            'reason' => Yii::t('app', '审核原因'),
            'link' => Yii::t('app', '文章信息外链'),
            'content' => Yii::t('app', '详细介绍'),
            'seocontent' => Yii::t('app', 'SEO描述'),
            'seokeyword' => Yii::t('app', 'SEO关键字'),
            'display' => Yii::t('app', '审核状态'),
            'status' => Yii::t('app', '状态'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '更新时间'),
            'member_id' => Yii::t('app', '会员名'), 
        ];
    }
    /**
     * @return \yii\db\ActiveQuery
     */
   /*  public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }
    public static function getArrayCategory()
    {
        return ArrayHelper::map(Category::find()->all(), 'id', 'name');
    } */
     /**
     * @inheritdoc
     */
    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = array_key_exists($this->status,self::getArrayStatus()) ? $statuses[$this->status] : '无';
        }
        return $this->_statusLabel;
    }
   /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [
            self::STATUS_ACTIVE => Yii::t('app', 'STATUS_ACTIVE'),
            self::STATUS_DELETED => Yii::t('app', 'STATUS_INACTIVE'),

        ];
    }
	 /**
     * @inheritdoc
     */

    public function getGradeLabel()
    {

        if ($this->_gradeLabel === null) {
            $grade = self::getArrayDis();
			//var_dump($grade);die();
            $this->_gradeLabel =  array_key_exists($this->display,self::getArrayDis()) ? $grade[$this->display] : '无';
			//var_dump( $this->_gradeLabel);die();
        }
        return $this->_gradeLabel;
    }
	public static function getArrayDis()
    {
        return [
		    self::DIS_ACTIVE => Yii::t('app', '待审核'),
            self::DIS_SUCCESS => Yii::t('app', '已审核'),
            self::DIS_DELETED => Yii::t('app', '再审核'),

        ];
    }
	
    // 会员名字,后台会员新闻
    public static function getArrayName()
    {

        $rows = (new \yii\db\Query())
            ->select(['id', 'username'])
            ->from('member')
            ->all();
        foreach($rows as $t){
            $data[$t['id']] = $t['username'];
        }
        return $data;
    }

	public static function getMember($id){
	    if(Member::findOne($id)){
            return Member::findOne($id)->username;
        }else{
            return '';
        }

		
	}

}
